package org.hawk.gwt.ppc.scanner;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.List;

import junit.framework.TestCase;

import org.hawk.gwt.ppc.scanner.mock.Mock1;
import org.hawk.gwt.ppc.scanner.mock.Mock2;
import org.hawk.gwt.ppc.scanner.mock.Mock3;

public class SourcesLoaderTestCase extends TestCase {
	
	public void testFileSystem() throws MalformedURLException{
		checkSourcesLoader(
			new FileSystemSourcesLoader("./src"));
	}
	
	public void testJar() throws IOException{
		checkSourcesLoader(
			new JarSourcesLoader("./src/test.jar"));
	}
	
	public void testUniversal() throws IOException{
		UniversalSourcesLoader loader = new UniversalSourcesLoader(
				new String[]{"./src","./src/test.jar"});
		assertNotNull(loader.getClassPath());
		assertEquals(2, loader.getClassPath().size());
		assertNotNull(loader.getClassPath().get(0));
		assertNotNull(loader.getClassPath().get(1));
		assertEquals(FileSystemSourcesLoader.class,
				loader.getClassPath().get(0).getClass());
		assertEquals(JarSourcesLoader.class,
				loader.getClassPath().get(1).getClass());
	}

	private void checkSourcesLoader(SourcesLoader loader)
			throws MalformedURLException {
		List<String> classes = loader.getClasses("org.hawk.gwt.ppc.scanner.mock");
		assertNotNull(classes);
		assertEquals(3, classes.size());
		assertEquals(Mock1.class.getName(),classes.get(0));
		assertEquals(Mock2.class.getName(),classes.get(1));
		assertEquals(Mock3.class.getName(),classes.get(2));
		
		assertEquals(Arrays.asList(new String[]{
			"org.hawk.gwt.ppc.scanner.mock.sub1",
			"org.hawk.gwt.ppc.scanner.mock.sub2"}), 
			loader.getSubpackages("org.hawk.gwt.ppc.scanner.mock"));
		
		assertNotNull(loader.getClasses("org._gnu"));
		assertEquals(loader.getClasses("org.$gnu").size(), 0);
		
		try{
			loader.getClasses("org.hawk.gwt.ppc.scanner..mock");
			fail();
		}
		catch(IllegalArgumentException e){}
		
		try{
			loader.getClasses("org.hawk.gwt.ppc.scanner.9");
			fail();
		}
		catch(IllegalArgumentException e){}
		try{
			loader.getClasses("org.hawk.gwt.ppc.scanner.9zz");
			fail();
		}
		catch(IllegalArgumentException e){}
		
		assertNotNull(loader.getResource(
			"org/hawk/gwt/ppc/scanner/mock/test.xml"));
		
		assertNull(loader.getResource(
		"org/hawk/gwt/ppc/scanner/mock/tets.xml"));
	}
}
